﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.XPath;
using Mrsoft.Common;
using MuYue.Common;
using MuYue.Http;

namespace Mrsoft.UI
{
    partial class frmPasswordChange : Form
    {

        public frmPasswordChange(Config<string> user, Config<string> pass) {
            InitializeComponent();
            user.SetBind(txtUserName, "Text");
            pass.SetBind(txtOldPass, "Text");
            this.user = user;
            this.pass = pass;
        }

        Config<string> user;
        Config<string> pass;

        /// <summary>
        /// 提交修改密码的请求
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChange_Click(object sender, EventArgs e) {
            string newpass = txtNewPass.Text;
            if (string.IsNullOrEmpty(newpass) || newpass == pass.Value) {
                MessageBox.Show(this, "请输入正确的新密码", "密码修改服务");
            }
            else {
                if (CallWebAPI(newpass, "resetpassword")) {
                    ShowMessage("密码修改成功");
                    txtOldPass.Text = newpass;
                    txtNewPass.Text = "";
                }
            }
        }

        /// <summary>
        /// 关闭密码修改服务窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void systemControl1_CloseBtnClick(object sender, EventArgs e) {
            this.Close();
        }


        /// <summary>
        /// 通过调用WebAPI来实现服务器信息
        /// </summary>
        /// <param name="url">API网址</param>
        /// <param name="param">用于返回API信息</param>
        /// <returns></returns>
        public bool CallWebAPI(string newpass, string method) {
            using (Profiler.Step("答题联网")) {
                //构建完成api访问地址
                try {
                    var url = string.Format("{3}api?user={0}&pass={1}&method={4}&newpass={2}",
                        user.Value,
                        Utility.Md5Encoding(pass.Value),
                        newpass,
                        Mrsoft.Properties.Resources.website,
                        method);

                    var result = WebEngine.DownloadString(url);
                    var rs = Utility.ToXmlString(result);

                    if (string.IsNullOrEmpty(rs)) {
                        ShowMessage("连接服务器失败了!");
                        return false;
                    }

                    var xDoc = XDocument.Parse(rs);
                    if (xDoc.Root == null || xDoc.Root.Name.LocalName != "dati") {
                        ShowMessage("消息解析失败!");
                        return false;
                    }

                    if (!bool.Parse(xDoc.Root.Attribute("state").Value)) {
                        ShowMessage(xDoc.Root.Attribute("message").Value);
                    }
                    else {
                        return true;
                    }
                }
                catch (Exception ex) {
                    ShowMessage(ex.Message);
                }
                return false;
            }
        }

        /// <summary>
        /// 将结果通过消息框展示给用户
        /// </summary>
        /// <param name="strMsg"></param>
        private void ShowMessage(string strMsg) {
            MessageBox.Show(this, strMsg, "密码修改服务");
        }

    }
}
